/*
 * 文件描述：音乐播放器列表的每一项
 * 文件作用：实现音乐播放器列表的项类musicListItem
 * 文件创建时间：2017-4-3
 * 文件创建者：wsgalaxy@163.com
 * 文件最后修改时间：
 * 文件修改者：
 * 文件协议：GPL
 *
 */


#include <QPainter>
#include <QMouseEvent>
#include "musicListItem.h"

musicListItem::musicListItem(QWidget *parent) : QWidget(parent)
{
    this->setFixedHeight(40);
    //this->setMinimumWidth();
    this->setMinimumWidth(50);
    isMouseIn = 0;
    this->setMouseTracking(true);
    isDeletePointed = 0;
}

int musicListItem::setMusicData(musicData *d)
{
    data = d;
    update();
    return 1;
}

void musicListItem::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QColor color;
    QBrush brush;
    QPen pen;

    brush.setStyle(Qt::SolidPattern);

    if(isMouseIn == 0)
        color.setRgb(0x22, 0x22, 0x22, 0x22);
    else
        color.setRgb(0x22, 0x22, 0x22, 0xBB);
    painter.fillRect(this->rect(), color);


    if(isDeletePointed == 1)
    {
        color.setRgb(0xFF, 0, 0, 0xAA);
        pen.setWidth(0);
        pen.setColor(color);
        brush.setColor(color);
        painter.setPen(pen);
        painter.setBrush(brush);

        painter.drawEllipse(QPointF(this->width()-20, 20), 10, 10);
    }


    if(isMouseIn == 1)
    {
        pen.setWidth(2);
        pen.setCapStyle(Qt::RoundCap);
        color.setRgb(0x22, 0x22, 0x22, 0xEE);
        pen.setColor(color);
        painter.setPen(pen);

        painter.drawLine(QPointF(this->width()-25, 15),
                         QPointF(this->width()-15, 25));
        painter.drawLine(QPointF(this->width()-25, 25),
                         QPointF(this->width()-15, 15));

    }

    pen.setWidth(1);
    color.setRgb(0xFF, 0xFF, 0xFF);
    pen.setColor(color);
    painter.setPen(pen);

    QFont font;
    font.setPixelSize(18);
    painter.setFont(font);

    painter.drawText(QRectF(12.5, 10, this->width()-45, 25),
                     Qt::AlignLeft | Qt::AlignVCenter,
                     data->name);




}


void musicListItem::mousePressEvent(QMouseEvent* event)
{
    if(event->x() >= this->width()-27.5 && event->x() <= this->width()-12.5
            && event->y() >= 12.5 && event->y() <= 27.5)
    {
        emit deleteMusic(data);
    }
}

void musicListItem::mouseMoveEvent(QMouseEvent* event)
{
    isMouseIn = 1;

    if(event->x() >= this->width()-27.5 && event->x() <= this->width()-12.5
            && event->y() >= 12.5 && event->y() <= 27.5)
    {
        isDeletePointed = 1;
        this->setCursor(Qt::PointingHandCursor);
    }
    else
    {
        this->setCursor(Qt::ArrowCursor);
        isDeletePointed = 0;
    }

    update();
}

void musicListItem::enterEvent(QEvent* event)
{
    isMouseIn = 1;
    update();
}

void musicListItem::leaveEvent(QEvent* event)
{
    isMouseIn = 0;
    update();
}

void musicListItem::mouseDoubleClickEvent(QMouseEvent* event)
{
    if(event->x() >= this->width()-27.5 && event->x() <= this->width()-12.5
            && event->y() >= 12.5 && event->y() <= 27.5)
    {
        return;
    }
    else
    {
        emit playMusic(data);

    }
}
